//
//  ScoreNumberWidget.cpp
//  FruitCrush
//
//  Created by gaoyu on 15/7/4.
//
//

#include "ScoreNumberWidget.h"

USING_NS_CC;

#define NUMX   0
#define NUMY   0
#define SHOWTIME 0.5
#define YPADMAX  20
#define NORMAL_FONTSIZE   25
#define STRONG_FONTSIZE   35
#define TEXT_FONTNAME     "arial"

ScoreNumberWidget::ScoreNumberWidget()
:numLabel(NULL)
,lastTime(0)
{
}

bool ScoreNumberWidget::init()
{
    if (!Layer::init()) {
        return false;
    }
    
    numLabelInit();
    scheduleUpdate();
    
    return true;
}

void ScoreNumberWidget::numLabelInit()
{
    numLabel = LabelTTF::create("0", TEXT_FONTNAME, NORMAL_FONTSIZE);
    numLabel->setAnchorPoint(Point(0, 0));
    numLabel->setPosition(NUMX, NUMY);
    numLabel->setColor(Color3B::RED);
    addChild(numLabel);
}

void ScoreNumberWidget::setNum(int num)
{
    numLabel->setString(Value(num).asString());
    numLabel->setPosition(NUMX-numLabel->getContentSize().width/2,NUMY);
}

void ScoreNumberWidget::setStrongNum(int num)
{
    removeAllChildren();
    numLabel = LabelTTF::create(Value(num).asString(), TEXT_FONTNAME, STRONG_FONTSIZE);
    numLabel->setAnchorPoint(Point(0, 0));
    numLabel->setPosition(NUMX-numLabel->getContentSize().width/2,NUMY);
    numLabel->setColor(Color3B::RED);
    addChild(numLabel);
}

void ScoreNumberWidget::setHealthNum(int num)
{
    removeAllChildren();
    __String *numStr = __String::create("");
    numStr->initWithFormat("+%d",num);
    numLabel = LabelTTF::create(numStr->getCString(), TEXT_FONTNAME, STRONG_FONTSIZE);
    numLabel->setAnchorPoint(Point(0, 0));
    numLabel->setPosition(NUMX-numLabel->getContentSize().width/2,NUMY);
    numLabel->setColor(Color3B::GREEN);
    addChild(numLabel);
}

void ScoreNumberWidget::update(float dt)
{
    if(lastTime < SHOWTIME){
        lastTime += dt;
        numLabel->setPosition(numLabel->getPosition().x,YPADMAX*lastTime/SHOWTIME);
    }else{
        this->removeFromParent();
    }
}
